---
title: "Emmanuel Maduneme"
output:
flexdashboard::flex_dashboard:
theme:
version: 4
bootswatch: minty
orientation: columns
vertical_layout: fill
source_code: embed
---
```{r setup, warning=FALSE, include=FALSE}
library(pacman)
p_load(tidyverse, esquisse, dlookr, rio, here, readr, tidytext, knitr, janitor, expss, hrbrthemes, flextable, labelled, sjlabelled, colorblindr, forcats, treemap, flexdashboard, treemap, plotly, ragg, extrafonts)
here()
```
```{r, include=FALSE}
## Importing Data
afrobarometer_ngr <- import(here("afrobarometer_release-data_nig_r8_en_2021-03-31.sav"),
setclass = "tbl_df")
NGA1 <- sf::st_read(here("untitled", "gadm41_NGA_1.shp"))
selected_afrob <- afrobarometer_ngr %>%
select(RESPNO, REGION, Q101:Q103, Q13:Q15C, Q16A, starts_with("Q41"), Q50A:Q50P_NIG, Q55A:Q56)
selected_afrob <- selected_afrob %>%
mutate(states = REGION)
```
```{r, trust, warning=FALSE, include=FALSE}
## Recode Variables Trust
# Missing Not at all Just a little Somewhat A lot Don’t know
# NA 0 1 2 3 4
temp <- selected_afrob[,12:25]
temp[temp== -1] <- NA
temp[temp==8] <- NA
temp[temp== 9] <- 4
temp[temp==0 ] <- 0
temp[temp==1] <- 1
temp[temp==2] <- 2
temp[temp==3] <- 3
temp <- set_labels(temp, labels = c( "Don’t know" = 4, "Not at all" = 0,"Just a little" = 1, "Somewhat" = 2, "A lot" = 3))
selected_afrob[,12:25] <- temp
## look_for(selected_afrob) This dislays all the labels
## Create Subset of dataset on Trust
temp2 <- selected_afrob %>%
select(starts_with("Q41"))
## Renaming Variables Trust
temp2 <- temp2 %>%
rename( "President" = "Q41A", "National Assembly" = "Q41B","INEC" = "Q41C",
"Elected lG Council" = "Q41D", "The Ruling Party (APC)" = "Q41E",
"Opposition Parties" ="Q41F", "Police" = "Q41G",
"Military/Army" = "Q41H", "Judiciary" = "Q41I",
"Tax/revenue officials" = "Q41J", "Traditional leaders" = "Q41K",
"Religious leaders" = "Q41L","State Governor" = "Q41M_NIG",
"State Legislature" = "Q41N_NIG")
selected_afrob[,12:25] <- temp2
# Missing Not at all Just a little Somewhat A lot Don’t know
# NA 0 1 2 3 4
## Clean data for plotting
Trust_Manipulation <- temp2 %>%
pivot_longer(
cols = c(1:14),
names_to = "institutions",
values_to = "trust_scores") %>%
#Not at all/ Just a little = 1, Somewhat = 2, A lot/Don't know = 3
mutate(trust_scores_recd = case_when(trust_scores < 1 ~ 1,
trust_scores == 2 ~ 2,
trust_scores > 2 ~ 3)) %>%
group_by(institutions, trust_scores) %>%
na.omit()
glimpse(Trust_Manipulation)
## Stacked Bar chart for trust in different institutions
trust_plot <- Trust_Manipulation %>%
ggplot(aes(reorder(institutions, trust_scores_recd)))+
geom_bar(aes(fill = as.factor(trust_scores_recd)), position="fill") +
coord_flip() +
scale_fill_manual(values = c('#8da0cb','#fc8d62','#66c2a5'),
labels=c("Not at all/ A little", "Somewhat", "Alot")) +
scale_y_continuous(labels = scales::percent) +
theme_void() +
theme(axis.line = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_text(size = 10, family="Times", face="bold", hjust = 1, color = "grey10"),
axis.text.x = element_text(size = 10, color = "grey10"),
panel.grid.major.x = element_line(color = "grey70",
size = 1.0,
linetype = 3),
legend.title=element_blank()) +
labs(x = "",
y = "Frequency",
caption = "Data from Afrobarometer 2022 Round 8 Nigeria")
```
About Data and Visualization {.sidebar}
======================================
The `afrobarometer_ngr` data is a publicly available data set from [**Afrobarometer**](https://www.afrobarometer.org/data/), an non-profit African survey organization based in Ghana. According to their website, they "conducts public attitude surveys on democracy, governance, the economy, and society." The data set is part of their many data sets from African countries.
Specifically, the `afrobarometer_ngr` data set is a 2022 8th round comprehensive survey on several topics from perceptions of democracy, elections and the media to attitudes towards different institutions in Nigeria.
Trust in Institutions
=====================================
Column {data-width=400}
-----------------------------------------
### Nigerian Trust Religious and Traditional Leaders more
```{r, warning=FALSE}
trust_plot
```
Column {data-width=200}
-----------------------------------------
```{r, echo=FALSE, message=FALSE, warning=FALSE}
## trust by States
Trust_by_states <- selected_afrob %>%
## characterize() %>%
mutate(states = REGION) %>%
select(48, 12:25) %>%
to_character(states) %>%
rename( "President" = "Q41A", "National Assembly" = "Q41B","INEC" = "Q41C",
"Elected lG Council" = "Q41D", "The Ruling Party (APC)" = "Q41E",
"Opposition Parties" ="Q41F", "Police" = "Q41G",
"Military/Army" = "Q41H", "Judiciary" = "Q41I",
"Tax/revenue officials" = "Q41J", "Traditional leaders" = "Q41K",
"Religious leaders" = "Q41L","State Governor" = "Q41M_NIG",
"State Legislature" = "Q41N_NIG")
Trust_by_states$states <- str_to_sentence(Trust_by_states$states)
Trust_by_states <- Trust_by_states %>%
mutate(NAME_1 = case_when(states == "Fct abuja" ~ "Federal Capital Territory",
states == "Abia" ~ "Abia",
states == "Cross river" ~ "Cross River",
TRUE ~ (states)))
Trust_President <- Trust_by_states %>%
group_by(NAME_1) %>%
tally(President) %>%
arrange(desc(n))
Trust_Religious_Leaders <- Trust_by_states %>%
group_by(NAME_1) %>%
tally(`Religious leaders`) %>%
arrange(desc(n))
Trust_President$n <- as.numeric(Trust_President$n)
Trust_Religious_Leaders$n <- as.numeric(Trust_Religious_Leaders$n)
Trust_President <- left_join(NGA1, Trust_President)
Trust_Religious_Leaders <- left_join(NGA1, Trust_Religious_Leaders)
Trust_President_plot <- Trust_President %>%
mutate(prop_score = n/sum(n, na.rm = T)) %>%
ggplot() +
geom_sf(aes(fill = prop_score))+
scale_fill_continuous(trans = 'reverse',
na.value = "white",
breaks = c(0.03, 0.06,0.09),
labels = c("Low Trust", "Mid Trust", "High Trust")) +
theme_void() +
labs(title = "Trust in the President by State",
subtitle = "Share of Nigerians who say they they trust A lot",
x = "",
y = "",
caption = "Data from Afrobarometer 2022 Round 8 Nigeria")
Trust_Religious_Leaders_plot <- Trust_Religious_Leaders %>%
mutate(prop_score = n/sum(n, na.rm = T)) %>%
ggplot() +
geom_sf(aes(fill = prop_score))+
scale_fill_gradient(low = "#fee6ce",
high = "#e6550d",
space = "Lab",
na.value = "white",
guide = "colourbar",
aesthetics = "fill",
breaks = c(0.03, 0.06,0.09),
labels = c("Low Trust", "Mid Trust", "High Trust")) +
theme_void()
```
### Trust in President by State
```{r warning=FALSE}
Trust_President_plot
```
### Trust in Religious Leaders by State
```{r warning=FALSE}
Trust_Religious_Leaders_plot
```
Issue Perceptions
=====================================
```{r, include=FALSE}
## First I created a subset of data that included all the issues interest.
percep <- selected_afrob %>%
select(starts_with("Q50"))
## I then recoded the variables so they can be calculated.
percep[percep== -1] <- NA
percep[percep==8] <- NA
percep[percep== 9] <- 5
percep[percep==0 ] <- 0
percep[percep==1] <- 1
percep[percep==2] <- 2
percep[percep==3] <- 3
percep[percep==4] <- 4
## These are the new values:
## Missing Very badly Fairly badly Fairly well Very well Don't know
# NA 1 2 3 4 5
percep <- set_labels(percep, labels = c("Missing" = NA,
"Very badly" = 1,
"Fairly badly" = 2,
"Fairly well" = 3,
"Very well" = 4,
"Don't know" = 5))
## I then joined it to the original dataset
selected_afrob[,26:41] <- percep
## Here I renamed the subset created so they are identifiable
percep2 <- percep %>%
rename("Economy" = "Q50A",
"Living Conditions" = "Q50B", "Job Creation" = "Q50C",
"Keeping Prices" = "Q50D",
"Narrowing Income Gap" ="Q50E",
"Crime Reduction" = "Q50F",
"Basic Health Services" = "Q50G",
"Education" = "Q50H",
"Water and Santiation" = "Q50I",
"Fighting Corruption" = "Q50J",
"Infrastructure" = "Q50K",
"Electricity" = "Q50L",
"Resolving Violent Crime" = "Q50M",
"Needs of Youths" = "Q50N",
"RIghts & Opportunities for Disabled People" = "Q50O",
"Addressing Armed Extremism" = "Q50P_NIG")
## I pivot longer in order to have the columns as values
percep3 <- percep2 %>%
select(1:3, 6, 7, 8, 10, 12, 16) %>%
pivot_longer(
cols = c(1:9),
names_to = "Issues",
values_to = "ratings")
## This was a bit redundant but.... yeah.
j <- percep3 %>%
group_by(Issues) %>%
summarize(avg_score = round(mean(ratings), digits = 2)) %>%
arrange(desc(avg_score))
## Here I began plotting the first bar chart
issue_ratings <- j %>%
group_by(Issues) %>%
ggplot(aes(reorder(Issues, avg_score), avg_score)) +
geom_col(aes(fill = Issues),size=4) +
ylim(0.0, 3.0) +
coord_flip() +
geom_text(aes(label = avg_score), hjust = -0.5) +
scale_fill_manual(values = c("#88CCEE", "#CC6677", "#DDCC77", "#117733", "#332288", "#AA4499", "#44AA99", "#999933", "#882255", "#661100", "#6699CC", "#888888")) +
theme_void() +
theme(axis.line = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_text(size = 10, family="Times",
face="bold", hjust = 1, color = "grey10"),
panel.grid.major.x = element_line(color = "grey90",
size = 0.5,
linetype = 1)) +
labs(title = "",
x = "",
y = "Average Ratings (1 = Very Badly, 4 = Very Well)" ) +
theme(legend.position="none",
plot.title=element_text(family="Times", face="bold", size=10),
axis.title.x = element_text(family = "Comic Sans MS", face="bold", size=10),
axis.title.y = element_text(family = "Times New Roman", face="bold"))
```
Column {data-width=400}
-----------------------------------------
### **Average Ratings of How Well the Federal Govt is Handling the Following**
```{r, warning=FALSE}
issue_ratings
```
General Media Use {data-orientation=rows}
=====================================
```{r,warning=FALSE, include=FALSE}
## As previously noted, a subset of media use columns, but but first, I changed the name
selected_afrob <- selected_afrob %>%
mutate(states = REGION)
## Then... the subset...
media_use <- selected_afrob %>%
select(Q55A:Q55E)
## Recoding values
media_use[media_use== -1] <- NA
media_use[media_use==8] <- NA
media_use[media_use== 9] <- 5
media_use[media_use==0 ] <- 0
media_use[media_use==1] <- 1
media_use[media_use==2] <- 2
media_use[media_use==3] <- 3
media_use[media_use==4] <- 4
## Changed labels
media_use <- set_labels(media_use, labels = c("Missing" = NA,
"Never" = 0,
"Less than once a month" = 1,
"A few times a month" = 2,
"A few times a week" = 3,
"Every day" = 4,
"Don't know" = 5))
## Used this code to check the labels
### val_lab(media_use$Q55A)
## Renamed the columns
media_use <- media_use %>%
rename("Radio" = "Q55A",
"Television" = "Q55B",
"Newspaper" = "Q55C",
"Internet" = "Q55D",
"Social Media" = "Q55E")
## Joined back to the original
selected_afrob[,42:46] <- media_use
### Some data wrangling in preparation for the plot.
media_use_main <- selected_afrob %>%
select(1, 2, 48, 42:46) %>%
rename("ID" = "RESPNO",
"REGION" = "REGION",
"states" = "states",
"Radio" = "Q55A",
"Television" = "Q55B",
"Newspaper" = "Q55C",
"Internet" = "Q55D",
"Social Media" = "Q55E") %>%
to_character(REGION, states)
## Changed the state names to a sentence case
media_use_main$states <- str_to_sentence(media_use_main$states)
### Some more data wrangling so I can plot the data in Tableau.
media_use_main <- media_use_main %>%
mutate(states = case_when(states == "Fct abuja" ~ "Federal Capital Territory",
states == "Abia" ~ "Abia",
states == "Cross river" ~ "Cross River",
TRUE ~ (states)),
state = states)
p_load(naijR)
# Create a data frame and view top rows
ss <- states()
numStates <- length(ss)
vv <- sample(LETTERS[1:5], numStates, TRUE)
Nigerian_states <- tibble(states = ss, letter = vv)
dd <- data.frame(state = ss, letter = vv)
#(search for useful packages scripts to find full_set)
full_set <- merge(x = dd, y = media_use_main, by = "state")
## Some more wrangling in preparation for plotting as well as for Tableau
full_set2 <- full_set %>%
select(1, 6:10) %>%
pivot_longer(
cols = c(2:6),
names_to = "media_type",
values_to = "rating"
) %>%
mutate(usage = case_when(rating == 5 ~ 1,
rating == 0 ~ 0,
rating == 1 ~ 2,
rating == 2 ~ 3,
rating == 3 ~ 4,
rating == 4 ~ 5)) %>%
group_by(media_type, state) %>%
summarise(avg_use = round(mean(usage, na.rm = T), digits = 3),
media_type = as.factor(media_type)) %>%
add_column(Country = "Nigeria") %>%
select(4, everything())
## Wrangling some more data, essentially, converting columsn into rows, recoding values, adding the country as a column to allow for plotting on Tableau
mediatype_state <- full_set %>%
select(1, 6:10) %>%
pivot_longer(
cols = c(2:6),
names_to = "media_type",
values_to = "rating"
) %>%
mutate(usage = case_when(rating == 5 ~ 1,
rating == 0 ~ 0,
rating == 1 ~ 2,
rating == 2 ~ 3,
rating == 3 ~ 4,
rating == 4 ~ 5)) %>%
mutate(media_type = as.factor(media_type)) %>%
add_column(Country = "Nigeria") %>%
select(-3)
## A plot to discover which media is most used
media_by_state <- full_set2 %>%
ggplot(aes(fct_rev(fct_relevel(media_type, "Radio", "Television",
"Newspaper", "Internet", "Social Media")), avg_use)) +
geom_col(aes(fill = media_type)) +
ylim(0.0, 5.0) +
# facet_wrap(~ state) +
scale_fill_OkabeIto(name = "media_type") +
coord_flip() +
theme_classic() +
theme(axis.line = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_text(size = 10, family="Times",
face="bold", hjust = 1,
color = "grey10"),
legend.position = "none",
panel.grid.major.x = element_line(color = "grey90",
size = 0.5,
linetype = 1)) +
labs(title = "",
x = "",
y = "Average Ratings (1 = Never, 5 = Every day)") +
theme(legend.position="none",
plot.title=element_text(family="Times", face="bold", size=10),
axis.title.x = element_text(family = "Comic Sans MS", face="bold", size=10),
axis.title.y = element_text(family = "Times New Roman", face="bold"))
```
Row {data-height=400}
-----------------------------------------
### Radio Remains the Main Source of News in Nigeria
```{r warning=FALSE}
media_by_state
```
```{r, warning=FALSE, echo=TRUE, eval=FALSE, include=FALSE}
## Just for Tableau: Attaching geographic data to media usage in order to create graphs on Tableau
tablaeu <- full_set %>%
add_column(Country = "Nigeria") %>%
rename(Social_Media = "Social Media") %>%
select(1, 6:9, 11) %>%
pivot_longer(
cols = c(2:5),
names_to = "media_type",
values_to = "ratings") %>%
mutate(ratings_c = as.character(ratings))
```
### Media Use By State
```{r warning=FALSE}
knitr::include_graphics(here( "final draft graphics", "Average Media Use by State.png"))
```
Radio & TV {data-navmenu="Media Use by State"}
=====================================
Row {data-height=400}
-----------------------------------------
### Radio Use By State
```{r, warning=FALSE}
knitr::include_graphics(here( "final draft graphics", "Radio By State.png"))
```
### TV Use By State
```{r, warning=FALSE}
knitr::include_graphics(here( "final draft graphics", "TV By State.png"))
```
Newspaper & Social Media {data-navmenu="Media Use by State"}
=====================================
Row {data-height=200}
-----------------------------------------
### Newspaper Use By State
```{r, warning=FALSE}
knitr::include_graphics(here( "final draft graphics", "Newspaper Use.png"))
```
### Social Media Use By State
```{r, warning=FALSE}
knitr::include_graphics(here( "final draft graphics", "Soc_Media by State.png"))
```
```
Social Media Use By State
```